package 排序;

/**
 * @Author Weton Li
 * @Date 2021/4/8 12:47
 */
public class 冒泡排序 {

    public static void bubbleSort(int[] nums) {

        int n = nums.length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - 1 - i; j++) { // 每一轮固定一个最大值，少比较一次
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }

    }


    public static void swap(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }

    public static void main(String[] args) {
        int[] input = {1, 3, 2, 4, 5, 9, 1};
        bubbleSort(input);
        for (int i : input) {
            System.out.print(i + "\t");
        }
    }
}
